module.exports = function ($scope, $resource, $modalInstance, $modal, toaster, obj, $state) {

    $scope.role_list = [];
    if(obj){
        $scope.bean = {
            'code': obj.code,
            'description': obj.description,
            'id': obj.id,
            'is_try': obj.is_try,
            'name': obj.name,
            'price_level1': obj.price_level1,
            'price_level2': obj.price_level2,
            'price_level3': obj.price_level3,
            'sort': obj.sort,
            'img' : '',
            'try_img' : obj.try_img || ''
        }
        $scope.array = $scope.bean.try_img.split(',');
        console.log($scope.array);
        if(obj.roles){
            var roles = obj.roles.split(',');
            var rolename = obj.role_name.split(',');
            for(var i = 0; i < roles.length; i++){
                $scope.role_list.push({'name': rolename[i], 'value': roles[i]});
            }
        }
    } else {
        $scope.bean = {
            'add': true,
            'sort': 1
        }
    }

    $scope.ok = function(){
        if(!$scope.bean.code){
            toaster.warning({title: '', body: '请输入套餐编号'});
            return;
        }
        if(!$scope.bean.name){
            toaster.warning({title: '', body: '请输入套餐名称'});
            return;
        }
        if($scope.role_list.length > 0){
            var temproleid = '', temprolename = '';
            angular.forEach($scope.role_list, function(item){
                temproleid += item.value + ',';
                temprolename += item.name + ',';
            });
            $scope.bean.roles = temproleid.substring(0, temproleid.length-1);
            $scope.bean.role_name = temprolename.substring(0, temprolename.length-1);
        }
        $scope.bean.price_level1 = $scope.bean.price_level1 || 0;
        $scope.bean.price_level2 = $scope.bean.price_level2 || 0;
        $scope.bean.price_level3 = $scope.bean.price_level3 || 0;

        $resource('/api/ac/pc/packageService/savePackage', {}, {}).save($scope.bean, function(res){
			if(res.errcode === 0){
                $modalInstance.close($scope.fromDate);
			} else {
				toaster.error({title: '', body: res.errmsg});
			}
		});
    }

	$scope.addRoles = function() {
		var modalInstance = $modal.open({
			template: require('../views/addRoles.html'),
			controller: 'addRoles',
			size: 'lg',
			resolve: {
				role_list: function () {
					return $scope.role_list;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result){
            $scope.role_list = result;
		});
    }

    $scope.removerole = function(index){
        $scope.role_list.splice(index,1);
    }

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }

    $scope.image = function(){
        var para = $state.get('app.imageupload');
        //设置上传文件夹，以自己业务命名
        angular.extend(para, {
            resolve : {  
                'dir' : function(){
                    return 't1';
                }
            } 
        })
        var modalInstance = $modal.open(para);
        modalInstance.opened.then(function() { // 模态窗口打开之后执行的函数  
            console.log('modal is opened');  
        });  
        modalInstance.result.then(function(result) {
            $scope.array = result;
            $scope.bean.try_img = $scope.array.join(',');
        }, function(reason) {
            console.log(reason);// 点击空白区域，总会输出backdrop  
            // click，点击取消，则会暑促cancel 
            $log.info('Modal dismissed at: ' + new Date());  
        }); 
    };
    $scope.del=function(index){
        $scope.array.splice(index,1);
        $scope.bean.try_img=$scope.array.join(',');
    }

};